package myjobs;

import ircbot.*;
import java.util.HashMap;
import java.util.Iterator;

/**
 * This is a matchmaking job forexample for online computer games.
 *
 * @author andreas
 */
public class MatchMakerJob extends AbstractJob {
    Bot bot;
    String channel;
    
    HashMap<String, Match> matches = new HashMap<String, Match>();

    /**
     * Construcs a matchmaker job.
     *
     * @param bot reference to a bot
     * @param channel the channel the job should exists in
     */
    public MatchMakerJob(Bot bot, String channel) {
        this.bot = bot;
        this.channel = channel;
    }

    @Override
    public void onMessage(String channel, String sender, String login, String hostname, String message) {
        if(message.startsWith("!")) {
            String subMessage = message.substring(1);
            String[] cmd = subMessage.split(" ");

            if(cmd[0].equalsIgnoreCase("mm")) {
                if(cmd[1].equalsIgnoreCase("new") && cmd.length == 4) {
                    String matchName = cmd[2];
                    int maxPlayers = Integer.parseInt(cmd[3]);

                    if(!matches.containsKey(matchName)) {
                        matches.put(matchName, new Match(maxPlayers));
                    }
                } else if(cmd[1].equalsIgnoreCase("join") && cmd.length == 3) {
                    String matchName = cmd[2];

                    if(matches.containsKey(matchName)) {
                        Match match = matches.get(matchName);

                        if(!match.hasPlayer(sender)) {
                            if(match.roomForMorePlayers()) {
                                match.addPlayer(sender);
                                if(!match.roomForMorePlayers()) {
                                    notifyPlayers(match);
                                }
                            }
                            else {
                                bot.sendMessage(channel, "Sorry, not room for more players :(");
                            }
                        }
                        else {
                            bot.sendMessage(channel, "You are already in this game");
                        }

                    }
                } else if(cmd[1].equalsIgnoreCase("showGames") && cmd.length == 2) {
                    printStatus();
                }
            }
        }
    }

    /**
     * Helper method for printing the status of the job.
     */
    private void printStatus() {
        for(Iterator<String> i = matches.keySet().iterator(); i.hasNext();) {
            String matchName = i.next();
            Match match = matches.get(matchName);

            String matchNameToBeSent = matchName +" ("+ match.getCurrentPlayers() +"/"+ match.getMaxPlayers() +")";
            bot.sendMessage(channel, matchNameToBeSent);
            for(Iterator<String> j = match.iterator(); j.hasNext();) {
                String playerName = j.next();
                bot.sendMessage(channel, " -"+playerName);
            }
        }
    }

    private void notifyPlayers(Match match) {
        for(Iterator<String> i = match.iterator(); i.hasNext();) {
            String player = i.next();

            bot.sendMessage(player, "Your game is ready!");
        }
    }
}
